# Master R code for statistical analyses performed for "Ethical Situations and their Effects on Judgments of Punishment"
# Ludwin-Peery, E., & Tingley, D.

f1<-read.csv(file="~/Data for Ludwin-Peery & Tingley.csv", skip=2, header=F) # You may need to change this filepath to wherever you are keeping the data


#--------------------------------------------------------------------
# Compares confirmation condition status to Political Punitiveness aggregates

id = f1[15]
id2 = as.character(unlist(id))
id3 = as.numeric(id2)
category = cut(id3,breaks=c(1,199,299))
levels(category) = c("1","0") # 1 is confirmation, 0 is no confirmation

wronga = f1[99]
wronga2 = as.character(unlist(wronga))
wronga3 = as.numeric(wronga2)
#aggregate "wrong" score

puna = f1[100]
puna2 = as.character(unlist(puna))
puna3 = as.numeric(puna2)
#aggregate "punishment" score

conpun = data.frame(category, wronga3, puna3)

conaovwrong = oneway.test(wronga3 ~ category, data=conpun, var.equal=T)
print("ANOVA Wrongness Scores and Confirmation Condition")
print(conaovwrong)

conaovpun = oneway.test(puna3 ~ category, data=conpun, var.equal=T)
print("ANOVA Punitiveness Scores and Confirmation Condition")
print(conaovpun)


#--------------------------------------------------------------------
# Cohen's d for wrongness and punishment

install.packages("lsr")

library(lsr)

print("Cohen's d for wrongness comparison")
cohenwrong = cohensD (wronga3 ~ category)
print(cohenwrong)

print("Cohen's d for punitiveness comparison")
cohenpun = cohensD (puna3 ~ category)
print(cohenpun)


#--------------------------------------------------------------------
# Correlation between judgments of wrongness and judgments of punishment

corwropun = cor.test(wronga3, puna3, method = c("pearson"))

print("Pearson's Punishment and Wrongness scores")
print(corwropun)


#--------------------------------------------------------------------
#T-Test for sex and judgments of wrongness & punishment

sex = f1[77]
sex2 = as.character(unlist(sex))
sex3 = as.numeric(sex2)
#Sex of Participant - Male is 1, Female is 2


sexpun = data.frame(sex3, wronga3, puna3)

mpun = subset(sexpun, sex3 == 1)
fpun = subset(sexpun, sex3 == 2)

malewrong = mpun[2]
malewrong2 = as.character(unlist(malewrong))
malewrong3 = as.numeric(malewrong2)
femalewrong = fpun[2]
femalewrong2 = as.character(unlist(femalewrong))
femalewrong3 = as.numeric(femalewrong2)
sexwro = t.test(femalewrong3,malewrong3,var.equal=TRUE)
print("T-Test Wrongness Scores and Sex")
print(sexwro)

malepun = mpun[3]
malepun2 = as.character(unlist(malepun))
malepun3 = as.numeric(malepun2)
femalepun = fpun[3]
femalepun2 = as.character(unlist(femalepun))
femalepun3 = as.numeric(femalepun2)
sexpun = t.test(femalepun3,malepun3,var.equal=TRUE)
print("T-Test Punitiveness Scores and Sex")
print(sexpun)


#--------------------------------------------------------------------
#Determines correlations for age and judgments of wrongness & punishment, as shown in table 1

age = f1[78]
age2 = as.character(unlist(age))
age3 = as.numeric(age2)
#age of participant


agewro = cor.test(age3, wronga3, alternative = c("less"), method = c("pearson"))

print("Pearson's Age and Wrongness scores")
print(agewro)

agepun = cor.test(age3, puna3, alternative = c("less"), method = c("pearson"))

print("Pearson's Age and Punitive scores")
print(agepun)